$().ready(function() {
	validateRule();
	/*var titlePage = $('#titlePage').val();
	if (null==titlePage || ''===titlePage){
		$('#titlePageBox').hide();
	} else {
		$('#titlePageBox').show();
	}*/
});

$.validator.setDefaults({
	submitHandler : function() {
		update();
	}
});
function update() {
	$.ajax({
		cache : true,
		type : "POST",
		url : ctx+"system/tMagazine/update",
		data : $('#signupForm').serialize(),// 你的formid
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {
			if (data.code == 0) {
				parent.layer.msg("操作成功");
				parent.reLoad();
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);

			} else {
				parent.layer.alert(data.msg)
			}

		}
	});

}

var validator;
var isExistence;
function validateRule() {
	jQuery.validator.addMethod("isExistence", function(value, param) {
		$.ajax({
			url : ctx+'system/tMagazine/unique', //唯一性
			method : 'post',
			data : {
				id : $("#id").val(),
				issn : $("#issn").val()
			},
			dataType : 'json',
			async : false,
			success : function(data) {
				isExistence = data;
			}
		});
		return !isExistence;
	}, $.validator.format("期刊号已存在"));
	var icon = "<i class='fa fa-times-circle'></i> ";
	validator = $("#signupForm").validate({
		rules : {
			title : {
				required : true
			},
			issn : {
				required : true,
				isExistence : true
			},
			remark : {
				maxlength : 200
			}
		},
		messages : {
			title : {
				required : icon + "请输入标题"
			},
			issn : {
				required : icon + "请输入期刊号",
				isExistence : icon + "期刊号已存在"
			},
			remark : {
				maxlength : icon + "最长200个字符"
			}
		}
	})
}

var mid=$('#id').val();
var title = $("#title").val();
var issn = $("#issn").val();
layui.use(['jquery','upload'], function () {
	var $ = layui.$;

	var upload = layui.upload;
	//执行实例
	var uploadInst = upload.render({
		elem: '#test1', //绑定元素
		url: ctx+'system/tMagazine/pdf/upload', //上传接口
		data: {
			id: mid,
			title: title,
			issn: issn
		},
		field: 'pdfFile',
		size: 102400,
		accept: 'file',
		exts: 'pdf',
		before: function(obj){
			title = $("#title").val();
			issn = $("#issn").val();
			if (''==title || ''==issn){
				$("#signupForm").submit();
				return false;
			}
			// this.data.id = $('#id').val();
			this.data.title = $('#title').val();
			this.data.issn = $('#issn').val();
			$('#test1').attr("disabled",'disabled');
			var html = '<i class="fa fa-cloud"></i>上传中...';
			$('#test1').html(html);
			return true;
		},
		done: function (r) {
			layer.msg(r.msg);
			$('#test1').removeAttr("disabled");
			var html = '<i class="fa fa-cloud"></i>上传pdf';
			$('#test1').html(html);
			// $('#id').val(r.id);
			$('#fileContent').val(r.filecontent);
			$('#filePages').val(r.filepages);
			$('#content').val(r.content);
		},
		error: function (r) {
			layer.msg(r.msg);
		}
	});
});


var app = new Vue({
	el: '#app1',
	data: {
		limit: 12,
		offset: 0,
		total: 0,
		file: '',
		type: '',
		rows: ''
	},
	methods: {
		getData: function () {
			$.getJSON(ctx+"system/tMagazine/pictures", {
				limit: this.limit,
				offset: this.offset,
				sort: 'page_no',
				order: 'asc',
				magazineId: $('#id').val(),
				type: this.type
			}, function (r) {
				app.total = r.total;
				app.rows = r.rows;
				if(app.total>0){
					app.page();
				}
			});
		},
		page: function () {
			var options = {
				currentPage: app.offset / 12 + 1, //当前页
				totalPages: Math.ceil(app.total / 12), //总页数
				numberofPages: 4, //显示的页数
				bootstrapMajorVersion: 3,
				alignment: 'center',
				size: 'large',
				shouldShowPage: true,
				itemTexts: function (type, page, current) { //修改显示文字
					switch (type) {
						case "first":
							return "首页";
						case "prev":
							return "上一页";
						case "next":
							return "下一页";
						case "last":
							return "尾页";
						case "page":
							return page;
					}
				},
				onPageClicked: function (event, originalEvent, type, page) {
					app.offset = (page - 1) * 12;
					app.getData();
				}
			};
			$('#page').bootstrapPaginator(options);
		},
		remove: function (id) {
			layer.confirm('确定要删除选中的记录？', {
				btn: ['确定', '取消']
			}, function () {
				$.ajax({
					url: ctx+"system/tMagazine/pictures/remove",
					type: "post",
					data: {
						'id': id,
						'type': 1
					},
					success: function (r) {
						if (r.code == 0) {
							layer.msg(r.msg);
							// $('#id').val(r.id);
							if (null!=r.title) {
								// $('#titlePage').val(r.title);
								// $('#fmImg').attr('src',r.title);
								// $('#fmBtn').attr('url',r.title);
								// $('#titlePageBox').show();
							}else {
								// $('#titlePageBox').hide();
							}
							app.getData();
						} else {
							layer.msg(r.msg);
							app.getData();
						}
					}
				});
			})
		},
		changeType: function (i) {
			this.type = i;
			this.offset = 0;
			this.getData();
		}
	},
	created: function () {
		this.changeType('')
	}
});



var clipboard = new Clipboard('button.copy', {
	text: function (trigger) {
		layer.msg('文件路径已复制到粘贴板');
		return $(trigger).attr('url');
	}
});

layui.use(['jquery','upload'], function () {
	var $ = layui.$;
	var upload1 = layui.upload;
	//执行实例
	var uploadInst1 = upload1.render({
		elem: '#test2', //绑定元素
		url: ctx+'system/tMagazine/pictures/upload', //上传接口
		data: {
			id: mid,
			title: title,
			issn: issn
		},
		size: 30720,
		field: 'picFile',
		accept: 'images',
		exts: 'jpg|png|jpeg',
		multiple: true,
		before: function(obj){
			title = $("#title").val();
			issn = $("#issn").val();
			if (''==title || ''==issn){
				$("#signupForm").submit();
				return false;
			}
			// this.data.id = $('#id').val();
			this.data.title = $('#title').val();
			this.data.issn = $('#issn').val();
			$('#test2').attr("disabled",'disabled');
			return true;
		},
		done: function (r) {
			layer.msg(r.msg);
			$('#test2').removeAttr("disabled");
			if (r.code==0){
				// $('#id').val(r.id);
				/*if (null!=r.title) {
					$('#titlePage').val(r.title);
					$('#fmImg').attr('src',r.title);
					$('#fmBtn').attr('url',r.title);
					$('#titlePageBox').show();
				}else {
					$('#titlePageBox').hide();
				}*/
			}
			app.getData();
		},
		error: function (r) {
			layer.msg(r.msg);
		}
	});
});

function removeTitle() {
	layer.confirm('确定要删除选中的记录？', {
		btn : [ '确定', '取消' ]
	}, function() {
		var id = $('#id').val();
		if (null==id){
			layer.msg('杂志对象不存在');
			return;
		}
		$.ajax({
			url : ctx+"system/tMagazine/pictures/remove",
			type : "post",
			data : {
				'id': id,
				'type': 0
			},
			success : function(r) {
				if (r.code==0) {
					layer.msg(r.msg);
					if (null!=r.title) {
						$('#fmImg').attr('src',r.title);
						$('#fmBtn').attr('url',r.title);
						$('#titlePageBox').show();
					}else {
						$('#titlePageBox').hide();
					}
					app.getData();
				}else{
					layer.msg(r.msg);
				}
			}
		});
	})
}

function removeContent() {
	layer.confirm('确定要清空图片？', {
		btn : [ '确定', '取消' ]
	}, function() {
		var id = $('#id').val();
		if (null==id){
			layer.msg('杂志对象不存在');
			return;
		}
		$.ajax({
			url : ctx+"system/tMagazine/pictures/remove",
			type : "post",
			data : {
				'id': id,
				'type': 2
			},
			success : function(r) {
				if (r.code==0) {
					layer.msg(r.msg);
					// $('#titlePageBox').hide();
					app.getData();
					$('#page').bootstrapPaginator({});
				}else{
					layer.msg(r.msg);
				}
			}
		});
	})
}